//
//  CHYYGLViewController.m
//  CarFile
//
//  Created by 陈浩 on 2018/1/18.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "CHYYGLViewController.h"
#import "CHReservationDetailController.h"
#import "CHSearchViewController.h"
#import "CHYYGLModel.h"
#import "CHSuperMenu.h"
#import "CHYYGLTableViewCell.h"
#define List_Width      (UIScreenW+1.4)/3.0
#define SelectColor     [UIColor colorWithRed:9/255.0 green:142/255.0 blue:221/255.0 alpha:1.0]
@interface CHYYGLViewController ()<UITableViewDelegate,UITableViewDataSource,CHSuperMenuDataSource,CHSuperMenuDelegate>

//@property (nonatomic, strong) LrdSuperMenu *menu;

@property (nonatomic, strong) CHSuperMenu *menu;

@property (nonatomic ,strong) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *datas;

@property (nonatomic ,assign) NSInteger index;   //  页数

@property (nonatomic, strong) NSArray *sort;

@property (nonatomic ,copy) NSString *areaid;

@property (nonatomic ,copy) NSString *state;

@property (nonatomic ,copy) NSString *startTime;

@property (nonatomic ,copy) NSString *endTime;

@property (nonatomic ,strong) NSMutableArray *areacns;

@end

@implementation CHYYGLViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"预约管理";
    self.view.backgroundColor = KRGB(230, 243, 252);
    
    [self setupRightBarButtonItem];
    self.areacns = [NSMutableArray arrayWithArray:[Globle getInstance].areacns];
    [self.areacns insertObject:@"全部" atIndex:0];
    
    
    // 数据
    
    self.sort = @[@"全部", @"未回复", @"已回复"];
    NSArray *titles = @[@"选择区域",@"选择状态",@"选择时间"];
    _menu = [[CHSuperMenu alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 44) withTitles:titles];
    _menu.delegate = self;
    _menu.dataSource = self;
    [self.view addSubview:_menu];
    
    
    [self setupTableView];
    
    
}

-(void)setupRightBarButtonItem{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 18 ,18);
    [btn setBackgroundImage:[UIImage imageNamed:@"home_yygl_search"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}


-(void)searchAction{
    
    CHSearchViewController *vc = [[CHSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}




//  建立列表UI
-(void)setupTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_menu.frame), UIScreenW, UIScreenH - 44 -64) style:UITableViewStylePlain];
    // 设置tableView的基本信息

    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    // 下拉刷新
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getNetworkData];
        
    }];
    //     上拉刷新
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
    [_tableView.mj_header beginRefreshing];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}


#pragma mark -- 网络请求数据
-(void)getNetworkData{
    _index = 1;
    [self.tableView.mj_footer endRefreshing];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.areaid) {
        params[@"areaid"] = self.areaid;
         params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }else{
        params[@"areaid"] = (NSString *)[Util getValue:@"areaid"];
         params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
    
    params[@"user"] = [Globle getInstance].user;
    params[@"state"] = self.state;
    params[@"begintime"] = self.startTime;
    params[@"endtime"] = self.endTime;
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"15";

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovsearchorder" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        NSLog(@"%@",result);
        [MBProgressHUD hideHUD];
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHYYGLModel *model = [CHYYGLModel objectWithKeyValues:dict];
     
                [self.datas addObject:model];
            }
            if (self.datas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            if (self.datas.count == 0) {
                
                self.tableView.placeholder = @"没有查询到预约记录";
                self.tableView.placeholderImage = @"data_null";
            }
            
            
        }else{
            
            self.tableView.placeholder = @"没有查询到预约记录";
            self.tableView.placeholderImage = @"data_null";
            //            [MBProgressHUD showError:<#(NSString *)#>]
            
        }
        
        [self.tableView reloadData];
        
    }];
    
    
}


#pragma mark --  加载更多数据
-(void)loadMoreData{
    _index++;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.areaid) {
        params[@"areaid"] = self.areaid;
         params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }else{
        params[@"areaid"] = (NSString *)[Util getValue:@"areaid"];
         params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    }
    params[@"user"] = [Globle getInstance].user;
    params[@"state"] = self.state;
    params[@"begintime"] = self.startTime;
    params[@"endtime"] = self.endTime;
    params[@"pagenum"] = [NSString stringWithFormat:@"%zd",_index];
    params[@"pagesize"] = @"15";
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovsearchorder" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [self.tableView.mj_footer endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            NSMutableArray *newDatas = [NSMutableArray array];
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHYYGLModel *model = [CHYYGLModel objectWithKeyValues:dict];
                
                [newDatas addObject:model];
            }
            if (newDatas.count < 15) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.datas addObjectsFromArray:newDatas];
            
        }else{
            
            self.tableView.placeholder = @"没有查询到预约记录";
            self.tableView.placeholderImage = @"data_null";
            
        }
        
        [self.tableView reloadData];
        
    }];
}



#pragma mark - UITableView数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHYYGLTableViewCell *cell = [CHYYGLTableViewCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.section];
    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor =KRGB(230, 243, 252);
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    跳转预约详情界面
    CHReservationDetailController *vc = [[CHReservationDetailController alloc] init];
    vc.model = self.datas[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
    
}


-(NSInteger)menu:(CHSuperMenu *)menu numberOfRowsInColumn:(NSInteger)column{
    if (column == 0) {
        
        
        return self.areacns.count;
    }else if(column == 1) {
        return self.sort.count;
    }
    return 0;
    
}


-(NSArray *)menu:(CHSuperMenu *)menu titleForRowInCoulumn:(NSInteger)column{
    if (column == 0) {
        return self.areacns;
    }else if(column == 1) {
        return self.sort;
    }
    return nil;
}


//点击
- (void)superMenu:(CHSuperMenu *)menu didSelectRowInCoulumn:(NSInteger)column backToResultDict:(NSMutableDictionary *)resutDic{
    

    if (!resutDic) {
        return;
    }
    self.areaid = resutDic[@"areaid"] ;
    self.state  = resutDic[@"state"] ;
    self.startTime = resutDic[@"starttime"];
    self.endTime = resutDic[@"endtime"];
    
    [MBProgressHUD showMessage:@"加载中..."];
    [self getNetworkData];
}


//#pragma mark  --   LrdSuperMenu 数据源方法
//- (NSInteger)numberOfColumnsInMenu:(LrdSuperMenu *)menu {
//    return 3;
//}
//
//- (NSInteger)menu:(LrdSuperMenu *)menu numberOfRowsInColumn:(NSInteger)column {
//    if (column == 0) {
//        return [[Globle getInstance].areacns  count];
//    }else if(column == 1) {
//        return self.sort.count;
//    }
//    return 0;
//}
//
//- (NSString *)menu:(LrdSuperMenu *)menu titleForRowAtIndexPath:(LrdIndexPath *)indexPath {
//    if (indexPath.column == 0) {
//        return [Globle getInstance].areacns[indexPath.row];
//    }else if(indexPath.column == 1) {
//        return self.sort[indexPath.row];
//    }
//    return nil;
//}



//- (void)menu:(LrdSuperMenu *)menu didSelectRowAtIndexPath:(LrdIndexPath *)indexPath {
//    if (indexPath.item >= 0) {
//        NSLog(@"点击了 %ld - %ld - %ld 项目",indexPath.column,indexPath.row,indexPath.item);
//    }else {
//        NSLog(@"点击了 %ld - %ld 项目",indexPath.column,indexPath.row);
//    }
//}
@end
